/** 下拉选择框 */
cc.Class({
    extends: cc.Component,

    properties: {
        /** 下拉框 */
        sp_box:cc.Sprite,
        /** 下拉列表节点 */
        sv_list:cc.ScrollView,
        /** 选择文本 */
        txt_sel:cc.Label,
        /** 选择索引 */
        selIndex:cc.Integer,
        /** 下拉内容 */
        d_dataV:[cc.String],
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.sv_list.node.active = false;
        this.sp_box.node.on(cc.Node.EventType.TOUCH_END, function(ev){
            this.sv_list.node.active = !this.sv_list.node.active;
        }, this);

        this.selIndex = 0;
        this.databind();

        this.sv_list.node.on(cc.Node.EventType.TOUCH_END, function(ev){
            let py = -this.sv_list.node.convertToNodeSpaceAR(ev.getLocation()).y;
            let ind = py/16|0;
            this.txt_sel.string = this.d_dataV[ind];
            this.selIndex = ind;
            this.sv_list.node.active = false;
            let ec = new cc.Event.EventCustom('combo', true);
            //ec.target = this;
            this.node.dispatchEvent(ec);
            //console.log("fff", py, ind, this.sv_list.node.height, this.sv_list.node.convertToNodeSpaceAR(ev.getLocation()).y);
        }, this);
    },

    /** 重新绑定数据 */
    databind(){
        this.sv_list.content.removeAllChildren();
        for(let i=0;i<this.d_dataV.length;i++){
            let nd = new cc.Node();
            nd.color = new cc.Color(142, 141, 123);
            nd.anchorY = 1;
            nd.x = 0;
            nd.y = -16*i-2;
            nd.name = i.toString();
            nd.width = 100;
            nd.height = 15;
            let lb = nd.addComponent(cc.Label);
            lb.string = this.d_dataV[i];
            lb.fontSize = 12;
            lb.lineHeight = 12;
            nd.parent = this.sv_list.content;
        }
        this.sv_list.node.height = 16 * this.d_dataV.length;
        this.sv_list.content.height = 16 * this.d_dataV.length;
        let v = cc.find("view", this.sv_list.node);
        v.height = 16 * this.d_dataV.length;
        if(this.d_dataV.length > 0){
            this.txt_sel.string = this.d_dataV[this.selIndex];
        }
    },

    start () {
        // for(let i=0;i<this.sv_list.content.childrenCount;i++){
        //     console.log("svlist", this.sv_list.content.children[i].name, this.sv_list.content.children[i].x, this.sv_list.content.children[i].y, this.sv_list.content.children[i].anchorX, this.sv_list.content.children[i].anchorY, this.sv_list.content.children[i].width, this.sv_list.content.children[i].height);
        // }
        // console.log("cont",this.sv_list.content.x,this.sv_list.content.y);

        // this.scheduleOnce(function(){
        //     for(let i=0;i<this.sv_list.content.childrenCount;i++){
        //         console.log("svlist_", this.sv_list.content.children[i].name, this.sv_list.content.children[i].x, this.sv_list.content.children[i].y, this.sv_list.content.children[i].anchorX, this.sv_list.content.children[i].anchorY, this.sv_list.content.children[i].width, this.sv_list.content.children[i].height);
        //         //let lb = this.sv_list.content.children[i].getComponent(cc.Label);
        //         //console.log("lb__", lb.overflow);
        //     }
        //     console.log("cont",this.sv_list.content.x,this.sv_list.content.y);
        // }, 5);
    },

    // update (dt) {},
});
